//
//  MainViewController.m
//  省市区_第四种
//
//  Created by 丁 on 15/1/10.
//  Copyright (c) 2015年 丁颖. All rights reserved.
//

#import "MainViewController.h"
#import "Province.h"
#import "City.h"
#import "Area.h"
#import "WeatherViewController.h"

@interface MainViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UITableView *tableViewPro;
@property (nonatomic, retain) UITableView *tableViewCity;
@property (nonatomic, retain) UITableView *tableViewArea;
@property (nonatomic, retain) NSIndexPath *indexPath;
@property (nonatomic, retain) NSIndexPath *indexPath1;
@end

@implementation MainViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
        [self handleData];
    }
    return self;
}

- (void)handleData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"area" ofType:@".txt"];
    NSString *dataStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"%@", dataStr);
    NSArray *dataArr = [dataStr componentsSeparatedByString:@"\n"];
//    NSLog(@"%@", dataArr);
    
    for (NSString *tempStr in dataArr) {
        
        if (![tempStr hasPrefix:@" "]) {
            Province *province = [[Province alloc] init];
            province.name = tempStr;
            [self.arr addObject:province];
            [province release];
            
        }else if ([tempStr hasPrefix:@"  "] && ![tempStr hasPrefix:@"   "]){
            City *city = [[City alloc] init];
            city.name = tempStr;
            Province *pro = [self.arr lastObject];
            [pro.distructArr addObject:city];
            [city release];
            
        }
    }
}

- (void)dealloc
{
    Block_release(_changeCityBlock);
    [_arr release];
    [_tableViewPro release];
    [_tableViewCity release];
//    [_tableViewArea release];
    [_indexPath release];
    [_indexPath1 release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableViewPro = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375/2, 667) style:UITableViewStylePlain];
    [self.view addSubview:self.tableViewPro];
    self.tableViewPro.dataSource = self;
    self.tableViewPro.delegate = self;
    [_tableViewPro release];
    
    self.tableViewCity = [[UITableView alloc] initWithFrame:CGRectMake(375 / 2 , 64, 375 / 2, 667) style:UITableViewStylePlain];
    [self.view addSubview:self.tableViewCity];
    self.tableViewCity.dataSource = self;
    self.tableViewCity.delegate = self;
    [_tableViewCity release];
    

    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    if (section == 0) {
//        return 2;
//    } else if (section == 1){
//        return 34;
//    }
//    return 0;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    if (section == 0) {
//        return 2;
//    }
//    else if (section == 1){
//        return 34;
//    }
    if (section == 0) {
        return 2;
    }
    else if (section == 2 && tableView == self.tableViewPro ) {
        return self.arr.count;
    }

    else{
        Province *pro = [self.arr objectAtIndex:self.indexPath.row];
        return pro.distructArr.count;

    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableViewPro) {
        static NSString *str = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
     }
        Province *pro = [self.arr objectAtIndex:indexPath.row];
        cell.textLabel.text = pro.name;
        return cell;
        
    }
    else if (tableView == self.tableViewCity){
        static NSString *str = @"reuse1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
        }
        Province *pro = [self.arr objectAtIndex:self.indexPath.row];
        City *city = [pro.distructArr objectAtIndex:indexPath.row];
        cell.textLabel.text = city.name;
        return cell;
        
    }
    return nil;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableViewPro deselectRowAtIndexPath:indexPath animated:NO];
    [self.tableViewCity deselectRowAtIndexPath:indexPath animated:NO];
    if (tableView == self.tableViewPro) {
        self.indexPath = indexPath;
        [self.tableViewCity reloadData];
        self.indexPath1 = 0;
        [self.tableViewArea reloadData];
    }
    self.str = [self.arr objectAtIndex:indexPath.row];
    if (tableView == self.tableViewCity) {
        NSString *cityName = [[[[self.arr objectAtIndex:self.indexPath.row] distructArr] objectAtIndex:indexPath.row] name];
        
        self.changeCityBlock(cityName);
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    

}

// 给表格添加索引
//- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
//{
//    NSMutableArray* titles = [[NSMutableArray alloc] initWithCapacity:26];
//    for (int i='A'; i<='Z'; i++) {
//        NSString* str = [NSString stringWithFormat:@"%i",i];
//        [titles addObject:str];
//    }
//    return titles;
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
